package com.cellulargames.view;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

import com.cellulargames.control.Controller;
import com.cellulargames.model.Carta;
import com.cellulargames.model.Figura;
import com.cellulargames.model.Font59;
import com.ramp.microswing.Graphics2D;


public class CardPainter {
	
	public int width;
	
	public int height;
	
	private Image cardBack;
	
	private Image currentMano[];
	
	private Image symbols[];
	
	private Font59 blackFont[];
	
	private Font59 redFont[];
	
	private String cardsName[] = {"", "", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};
	
	public CardPainter(int w, int h)throws Exception{
		this.width = w;
		this.height = h;		
		loadImages();
	}
	
	private void loadImages() throws Exception{
		
		cardBack = Image.createImage(width, height);
		Graphics2D.fillGradientRect(cardBack.getGraphics(), 0xCCCCCC, 0xFFFFFF, 1, 1, width-4, height-2, Graphics2D.HORIZONTAL_GRADIENT);		
		
		symbols = new Image[4];
		symbols[0] = Image.createImage("/diamante.png");
		symbols[1] = Image.createImage("/picas.png");
		symbols[2] = Image.createImage("/trebol.png");		
		symbols[3] = Image.createImage("/corazon.png");
	
		currentMano = new Image[5];
		
		currentMano[0] = Image.createImage(width, height);
		currentMano[1] = Image.createImage(width, height);
		currentMano[2] = Image.createImage(width, height);
		currentMano[3] = Image.createImage(width, height);
		currentMano[4] = Image.createImage(width, height);
		
		blackFont = new Font59[15];
		redFont = new Font59[15];
		
		for(int i=0;i<15;i++){
			blackFont[i] = new Font59("/salidanegra.fnt");
			redFont[i] = new Font59("/salidaroja.fnt");
		}
				
	}
	
	private int y1;
	
	private int y2;
	
	private int y3;
	
	private int y4;
	
	private int y5;
	
	public void paint(Graphics g2, int _x, int _y){
		
		y1 = (y1 == 0) ? _y : y1;
		y2 = (y2 == 0) ? _y : y2;
		y3 = (y3 == 0) ? _y : y3;
		y4 = (y4 == 0) ? _y : y4;
		y5 = (y5 == 0) ? _y : y5;
		
		if( VideoPoker.reloadCards ){
			
			int x = 0;			
			int y = 0;
		
			for( int i=0;i<5;i++ ){
				
				Graphics g = currentMano[i].getGraphics();
				
				Carta c = Controller.currentCartas[i];
				
				g.setColor(0x000000);
				g.fillRoundRect(x+2, y+2, width, height, 5, 5);
				
				g.setColor(0xFFFFFF);
				g.fillRoundRect(x, y, width, height, 5, 5);
				
				g.setColor(0x000000);
				g.drawRoundRect(x, y, width, height, 5, 5);
				
				g.drawImage(cardBack, x+1, y+1, Graphics.LEFT|Graphics.TOP);
				
				g.drawImage(
						symbols[c.figura], x+(width-symbols[c.figura].getWidth())/2, 
						y+(height-symbols[c.figura].getHeight())/2, Graphics.LEFT|Graphics.TOP
						);
				
				if( c.figura == Figura.TREBOL || c.figura == Figura.PICAS ){
					blackFont[c.valor].PrintString(g, x+4, y, cardsName[c.valor], Font59.LEFT, Font59.TOP);
					blackFont[c.valor].PrintString(
							g, x+(width-blackFont[c.valor].stringWidth(cardsName[c.valor]))-8, 
							y+(height-blackFont[c.valor].getFontHeight()), cardsName[c.valor], Font59.HCENTER, Font59.TOP
							);
				}
				else{
					redFont[c.valor].PrintString(g, x+4, y, cardsName[c.valor], Font59.LEFT, Font59.TOP);
					redFont[c.valor].PrintString(
							g, x+(width-redFont[c.valor].stringWidth(cardsName[c.valor]))-8, 
							y+(height-redFont[c.valor].getFontHeight()), cardsName[c.valor], Font59.HCENTER, Font59.TOP
							);
				}
			
			}
			
			VideoPoker.reloadCards = false;
			
			Controller.reset();
			
		}
		
		
		
		if( Controller.currentCartas[0].hold ){
			y1 = (y1 > _y-4) ? y1-1 : y1; 
			g2.drawImage(currentMano[0], _x, y1, Graphics.LEFT|Graphics.TOP);
		}
		else{
			y1 = (y1 < _y) ? y1+1 : y1;
			g2.drawImage(currentMano[0], _x, y1, Graphics.LEFT|Graphics.TOP);
		}
		_x+=width+2;
		
		if( Controller.currentCartas[1].hold ){
			y2 = (y2 > _y-4) ? y2-1 : y2; 
			g2.drawImage(currentMano[1], _x, y2, Graphics.LEFT|Graphics.TOP);
		}
		else{
			if( VideoPoker.doble ){
				g2.drawImage(VideoPoker.cardback, _x, _y, Graphics.LEFT|Graphics.TOP);
			}
			else{
				y2 = (y2 < _y) ? y2+1 : y2;
				g2.drawImage(currentMano[1], _x, y2, Graphics.LEFT|Graphics.TOP);
			}
		}
		_x+=width+2;
		
		if( Controller.currentCartas[2].hold ){
			y3 = (y3 > _y-4) ? y3-1 : y3; 
			g2.drawImage(currentMano[2], _x, y3, Graphics.LEFT|Graphics.TOP);
		}
		else{
			if( VideoPoker.doble ){
				g2.drawImage(VideoPoker.cardback, _x, _y, Graphics.LEFT|Graphics.TOP);
			}
			else{
				y3 = (y3 < _y) ? y3+1 : y3;
				g2.drawImage(currentMano[2], _x, y3, Graphics.LEFT|Graphics.TOP);
			}
		}
		_x+=width+2;
				
		if( Controller.currentCartas[3].hold ){
			y4 = (y4 > _y-4) ? y4-1 : y4; 
			g2.drawImage(currentMano[3], _x, y4, Graphics.LEFT|Graphics.TOP);
		}
		else{
			if( VideoPoker.doble ){
				g2.drawImage(VideoPoker.cardback, _x, _y, Graphics.LEFT|Graphics.TOP);
			}
			else{
				y4 = (y4 < _y) ? y4+1 : y4;
				g2.drawImage(currentMano[3], _x, y4, Graphics.LEFT|Graphics.TOP);
			}
		}
		_x+=width+2;
		
		if( Controller.currentCartas[4].hold ){
			y5 = (y5 > _y-4) ? y5-1 : y5; 
			g2.drawImage(currentMano[4], _x, y5, Graphics.LEFT|Graphics.TOP);
		}
		else{
			if( VideoPoker.doble ){
				g2.drawImage(VideoPoker.cardback, _x, _y, Graphics.LEFT|Graphics.TOP);
			}
			else{
				y5 = (y5 < _y) ? y5+1 : y5;
				g2.drawImage(currentMano[4], _x, y5, Graphics.LEFT|Graphics.TOP);
			}
		}
		_x+=width+2;

		
	}
	

}

